package com.yuanzi.base.auth;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.springframework.stereotype.Service;

import com.yuanzi.base.constants.AuthConstants;
import com.yuanzi.base.redis.OuterHashRedisService;
import com.yuanzi.base.redis.OuterStringRedisService;

/**
 * 此类用于对权限相关缓存的redis存取操作
 * @author cui
 * @data 2017年7月13日
 *
 */
@Service
public class AuthSchoolRedisService {
	
	private static String serviceName = AuthConstants.AUTH_SERVICE_NAME;
	
	/*--------------存储教学班对行政班的关系：Hash形式：zuul:teachclass_class:teachClassId(key):行政班Id(field)=>行政班Id(value)---------------*/
	/**
	 * 获取当前教学班中的行政班
	 * @author cui
	 * @param teachClassId 教学班Id
	 * @return
	 */
	public static Set<String> getTeachClassToClass(String teachClassId){
		return OuterHashRedisService.hkeys(serviceName, AuthConstants.REDIS_TEACHECLASS_CLASS+teachClassId);
	}
	
	/**
	 * 设置当前教学班中的行政班,value值暂时跟field值一样，有需要可以存别的
	 * @author cui
	 * @param teachClassId 教学班Id
	 * @param classId 行政班Id
	 */
	public static void setTeachClassToClass(Long teachClassId, Long classId){
		OuterHashRedisService.hset(serviceName, AuthConstants.REDIS_TEACHECLASS_CLASS+teachClassId.toString(), classId.toString(), classId.toString());
	}

	/**
	 * 设置当前教学班中的行政班,value值暂时跟field值一样，有需要可以存别的
	 * @author cui
	 * @param teachClassId 教学班Id
	 * @param classId 行政班Id
	 */
	public static void setTeachClassToClassByBatch(Long teachClassId, Long... classIds){
		Map<String, String> mapClasses = new HashMap<>();
		for(Long classId : classIds){
			mapClasses.put(classId.toString(), classId.toString());
		}
		OuterHashRedisService.hmset(serviceName, AuthConstants.REDIS_TEACHECLASS_CLASS+teachClassId.toString(), mapClasses);
	}
	
	/**
	 * 删除一个教学班中的行政班：当一个教学班修改行政班时用
	 * @author cui
	 * @param teachClassId
	 * @param classId
	 */
	public static void delTeachClassToClassByClassId(Long teachClassId, Long classId){
		OuterHashRedisService.hdel(serviceName, AuthConstants.REDIS_TEACHECLASS_CLASS+teachClassId.toString(), classId.toString());
	}
	
	/**
	 * 根据教学班Id删除教学班对行政班的关系
	 * @author cui
	 * @param teachClassId 教学班Id
	 */
	public static void delTeachClassToClassByTeachClassId(Long teachClassId){
		OuterHashRedisService.del(serviceName, AuthConstants.REDIS_TEACHECLASS_CLASS+teachClassId.toString());
	}
	
	
	/*--------------存储学生对应的行政班：Hash形式：zuul:student_class:studentMemberId(key):行政班Id(field)---------------*/
	/**
	 * 获取当前成员(学生)所属行政班
	 * @author cui
	 * @param memberId 学生成员Id
	 * @return
	 */
	public static String getClassOfMember(String memberId){
		return OuterStringRedisService.get(serviceName, AuthConstants.REDIS_STUDENT_CLASS, memberId);
	}
	
	/**                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  mn\,nmmmmmmmmmmm\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\nnnnnnnnnnnn                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   \\
	 * 设置当前成员(学生)所属行政班
	 * @author cui
	 * @param memberId 学生成员Id
	 * @param classId 行政班id
	 */
	public static void setClassOfMember(Long memberId, Long classId){
		OuterStringRedisService.set(serviceName, AuthConstants.REDIS_STUDENT_CLASS, memberId.toString(), classId.toString());
	}
	
	/**
	 * 批量设置成员(学生)所属行政班
	 * @author cui
	 * @param classId 行政班id
	 * @param memberId 学生成员Id数组
	 */
	public static void setClassOfMemberByBatch(Long classId, String... memberIds){
		for(String memberId : memberIds){
			OuterStringRedisService.set(serviceName, AuthConstants.REDIS_STUDENT_CLASS, memberId, classId.toString());
		}
	}
	
	/**
	 * 根据(学生)成员Id删除学生与行政班的关系
	 * @author cui
	 * @param memberId 学生成员Id
	 */ 
	public static void delClassOfMemberByMemberId(Long memberId){
		OuterStringRedisService.del(serviceName, AuthConstants.REDIS_STUDENT_CLASS, memberId.toString());
	}
	
	/**
	 * 批量删除学生与行政班的关系
	 * @author cui
	 * @param memberIds
	 */
	public static void delClassOfMemberByBatch(String[] memberIds){
		for(String memberId : memberIds){
			OuterStringRedisService.del(serviceName, AuthConstants.REDIS_STUDENT_CLASS, memberId);
		}
	}
	
	
	/*--------------存储教师对教学班的权限：Hash形式：zuul:teachclass_teacher:teachClassId(key):teacherMemberId(field)=>权限(value)---------------*/
	/**
	 * 获取教师在教学班中的权限(角色)
	 * @author cui
	 * @param teachClassId 
	 * @param memberId 教师成员id
	 * @return
	 */
	public static String getRightOfTeacherInTeachclass(String teachClassId, String memberId){
		return OuterHashRedisService.hget(serviceName, AuthConstants.REDIS_TEACHCLASS_TEACHER+teachClassId, memberId);
	}
	
	/**
	 * 设置教师在教学班中的权限(角色)
	 * @author cui
	 * @param teachClassId
	 * @param memberId 教师成员id
	 * @param right 教师对于教学班的权限：1：主讲  2:助教  3：创建人
	 */
	public static void setRightOfTeacherInTeachclass(Long teachClassId, Long memberId, String right){
		OuterHashRedisService.hset(serviceName, AuthConstants.REDIS_TEACHCLASS_TEACHER+teachClassId.toString(), memberId.toString(), right);
	}
	
	/**
	 * 批量设置教师在教学班中的权限(角色)
	 * @author cui
	 * @param teachClassId
	 * @param memberIdRightMap
	 */
	public static void setRightOfTeacherInTeachclassByBatch(Long teachClassId, Map<String, String> memberIdRightMap){
		OuterHashRedisService.hmset(serviceName, AuthConstants.REDIS_TEACHCLASS_TEACHER+teachClassId.toString(), memberIdRightMap);
	}
	
	/**
	 * 删除教师在教学班中的权限(角色)
	 * @author cui
	 * @param teachClassId
	 * @param memberId 教师成员id
	 */
	public static void delRightOfTeacherInTeachclassByMemberId(Long teachClassId, Long memberId){
		OuterHashRedisService.hdel(serviceName, AuthConstants.REDIS_TEACHCLASS_TEACHER+teachClassId.toString(), memberId.toString());
	}
	
	/**
	 * 根据教学班Id删除所有教师对该教学班的权限(包括创建者)
	 * @author cui
	 * @param teachClassId
	 */
	public static void delRightOfTeacherInTeachclassByBatch(Long teachClassId){
		OuterHashRedisService.del(serviceName, AuthConstants.REDIS_TEACHCLASS_TEACHER+teachClassId.toString());
	}
	
	/**
	 * 根据教学班Id删除所有教师对该教学班的权限(不包括创建者)
	 * @author cui
	 * @param teachClassId
	 * @param memberId 主讲成员Id 
	 */
	public static void delRightOfTeacherInTeachclassByBatch(Long teachClassId, Long memberId){
		OuterHashRedisService.del(serviceName, AuthConstants.REDIS_TEACHCLASS_TEACHER+teachClassId.toString());
		OuterHashRedisService.hset(serviceName, AuthConstants.REDIS_TEACHCLASS_TEACHER+teachClassId.toString(), memberId.toString(), AuthConstants.TEACHCLASS_RIGHT_CREATOR);
	}
}
